/**
 * 
 */
package cn.edu.cqu.bysj.wm.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.neo4j.graphdb.Node;

import cn.edu.cqu.bysj.wm.ago.anlys.ModularityLP;
import cn.edu.cqu.bysj.wm.ago.lp.LPCommunity;
import cn.edu.cqu.bysj.wm.def.ConstDef;
import cn.edu.cqu.bysj.wm.keynode.KeyNodes;

/**
 * @author Meng
 * 
 */
public class KeyNodesOuput {

	public static StringBuilder printBuilder = CommunityOutput.printBuilder;

	public static void setPrintBuilder() {
		printBuilder = CommunityOutput.printBuilder;
	}

	public static void globalOutput(List<Node> keyNodes, int type) {
		printBuilder
				.append("--------------------------------spearman------------------------------------------\n");
		printBuilder.append(KeyNodes.spearmanCoefficient + "\n");

		if (type == ConstDef.SKYLINE) {
			printBuilder
					.append("-----------------------skyline search----------------------------------------------\n");
			printBuilder.append("global total time:" + KeyNodes.sTimeGlobal
					+ "\n");
			printBuilder.append("global key nodes:" + keyNodes.size() + "\n");

		} else {
			printBuilder
					.append("-----------------------fagin search----------------------------------------------\n");
			printBuilder.append("global total time:" + KeyNodes.fTimeGlobal
					+ "\n");
			printBuilder.append("top K=" + keyNodes.size() + "\n");
		}

		for (int i = 0; i < keyNodes.size(); i++) {
			if (i % 25 == 0 && i != 0) {
				printBuilder.append("\n");
			}
			printBuilder.append(keyNodes.get(i).getId() + " ");
		}
		printBuilder.append("\n");

		// statistics global key nodes in communitys
		Map<Long, List<Node>> map = new HashMap<Long, List<Node>>();
		for (Node node : keyNodes) {
			if (ModularityLP.nodeCommunitiy.containsKey(node.getId())) {
				long cid = ModularityLP.nodeCommunitiy.get(node.getId());
				if (!map.containsKey(cid)) {
					map.put(cid, new ArrayList<Node>());
				}
				map.get(cid).add(node);
			} else {

			}
		}

		List<Long> list = new ArrayList<Long>(map.keySet());
		Collections.sort(list);

		for (long cid : list) {
			printBuilder.append(cid + ":\t");
			for (Node node : map.get(cid)) {
				int rank = keyNodes.indexOf(node) + 1;
				printBuilder.append(node.getId() + "(" + rank + ")\t");
			}
			printBuilder.append("\n");
		}

	}

	public static void localOutput(Map<Long, List<Node>> keynodes, int type) {

		if (type == ConstDef.SKYLINE) {
			printBuilder
					.append("-----------------------skyline search----------------------------------------------\n");
			printBuilder.append("local total time:" + KeyNodes.sTimeLocal
					+ "\n");
		} else {
			printBuilder
					.append("-----------------------fagin search----------------------------------------------\n");
			printBuilder.append("local total time:" + KeyNodes.fTimeLocal
					+ "\n");
		}

		for (long cid : keynodes.keySet()) {

			List<Node> list = keynodes.get(cid);

			printBuilder.append("cid" + cid + "\tlocal key nodes "
					+ list.size() + "\tcommunity size:"
					+ LPCommunity.communitys.get(cid).size() + "\n");

			for (int i = 0; i < list.size(); i++) {
				if (i % 10 == 0 && i != 0) {
					printBuilder.append("\n");
				}
				Node node = list.get(i);
				// if (type == ConstDef.FAGIN) {
				// int rank = KeyNodes.fGlobalKeyNodes.indexOf(node) + 1;
				// System.out.print(node.getId() + "(" + rank + ")" + "\t");
				// } else {
				System.out.print(node.getId() + "\t");
				// }
			}
			printBuilder.append("\n");
		}
	}
}
